package com.guigu.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.guigu.base.entity.GlobalUser;
import com.guigu.base.entity.GoodsType;
import com.guigu.base.entity.vo.OrderDashboardVO;
import com.guigu.common.request.HttpUrlUtils;
import com.guigu.order.entity.Order;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.order.entity.OrderCargo;
import com.guigu.order.entity.OrderLocation;
import com.guigu.order.entity.dto.OrderDTO;
import com.guigu.order.entity.query.OrderQuery;
import com.guigu.order.entity.results.AddressResult;
import com.guigu.order.entity.rules.AddressRule;
import com.guigu.order.feign.BaseFeign;
import com.guigu.order.feign.PayFeign;
import com.guigu.order.feign.WorkFeign;
import com.guigu.order.mapper.OrderMapper;
import com.guigu.order.service.OrderCargoService;
import com.guigu.order.service.OrderLocationService;
import com.guigu.order.service.OrderService;
import com.guigu.tms.exception.TmsException;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import com.guigu.work.dto.TaskPickupDispatchDTO;
import com.guigu.work.dto.TransportOrderDTO;
import com.guigu.work.entity.TaskPickupDispatch;
import com.guigu.work.entity.TransportOrder;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-09
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    private Logger log = LoggerFactory.getLogger(OrderServiceImpl.class);
    @Value("${ampap-ack}")
    private String secret;
    @Autowired
    private KieContainer kieContainer;
    @Autowired
    private WorkFeign workFeign;
    @Autowired
    private BaseFeign baseFeign;
    @Autowired
    private OrderLocationService orderLocationService;
    @Autowired
    private OrderCargoService orderCargoService;

    @Autowired
    private PayFeign payFeign;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 订单添加
     *
     * @param orderDTO
     */
    @Override
    @GlobalTransactional(name = "order_create_addOrder", rollbackFor = Exception.class)
    public void addOrder(OrderDTO orderDTO) {

        /***
         * 非空判断
         */
        notNull(orderDTO);
        /**
         * 设置商品总重量
         */
        orderDTO.getOrderCargo().setTotalWeight(new BigDecimal(orderDTO.getOrderCargo().getQuantity()).multiply(orderDTO.getOrderCargo().getWeight()));

        /**
         * 获取金额
         */
        getAmount(orderDTO);
        /**
         * 添加订单表
         * 如果是上门自取,则订单状态为1(待取件)
         * 如果是网点自寄,则订单状态为4(网点入库，待发车)
         */
        Order order = saveOrder(orderDTO);

        /**
         * 添加运单表
         */
        TransportOrder transportOrderDTO = saveTransportOrder(order);

        /**
         * 添加商品位置表
         */
        saveOrderLocation(orderDTO, order);

        /**
         * 添加商品总量信息表
         */
        saveOrderCargo(orderDTO, order, transportOrderDTO);
        /**
         * 发送消息
         */
        log.info("order为:{}", order.getId());
        payFeign.sendPayMessage(order.getId());
    }

    /**
     * 条件查询订单信息
     *
     * @param orderQuery :order对象
     * @param pageNum    页码
     * @param pageSize   页数
     * @return
     */
    @Override
    public R<Page<Order>> getOrderInfo(OrderQuery orderQuery, Integer pageNum, Integer pageSize) {
        Page<Order> selectPage = (Page<Order>) getPage(orderQuery, pageNum, pageSize);
        return new R<>(ResultCode.SUCCESS_CODE, selectPage);
    }

    /**
     * 条件查询订单状态
     *
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            * @param orderQuery
     * @param pageNum
     * @param pageSize
     * @return
     */
    private IPage<Order> getPage(OrderQuery orderQuery, Integer pageNum, Integer pageSize) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        IPage<Order> page = new Page();
        if (ObjectUtils.isEmpty(orderQuery)) {
            throw new TmsException(ResultCode.ERROR_CODE, "查询对象为空!");
        }
        if (pageNum <= 0 || pageNum == null) {
            pageNum = 1;
        }
        if (pageSize <= 0 || pageSize == null) {
            pageSize = 5;
        }
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        /**
         * 订单状态查询
         */
        if (orderQuery.getStatus() != null) {
            queryWrapper.eq("status", orderQuery.getStatus());
        }
        /**
         * 付款状态查询
         */
        if (orderQuery.getPaymentStatus() != null) {
            queryWrapper.eq("payment_status", orderQuery.getPaymentStatus());
        }
        IPage<Order> selectPage = this.baseMapper.selectPage(page, queryWrapper);
        return selectPage;
    }

    /**
     * 根据订单id查询订单重量信息和订单位置信息
     *
     * @param orderId 订单id
     * @return
     */
    @Override
    public R<Map> getOrderCargoVoAndOrderLocationVoInfoByOrderId(String orderId) {
        if (StringUtils.isBlank(orderId)) {
            throw new TmsException(ResultCode.ERROR_CODE, "订单Id为null");
        }
        Map<String, Object> map = new HashMap<>();

        /**
         * 查询订单
         */
        Order order = this.baseMapper.selectById(orderId);
        map.put("order", order);
        /**
         * 获取商品重量信息
         */
        OrderCargo cargo = getOrderCargo(orderId);
        map.put("orderCargo", cargo);
        /**
         * 获取商品消息
         */
        OrderLocation location = getOrderLocation(orderId);

        map.put("orderLocation", location);
        /**
         *运单状态信息
         */
        TransportOrder transportOrder = getTransportOrder(orderId);
        map.put("transportOrder", transportOrder);
        /**
         * 获取货物类型
         */
        GoodsType data = getPdGoodsType(cargo);

        /**
         * 获取取件信息
         */
        R<TaskPickupDispatch> dispatchR = workFeign.findByOrderId(orderId, 1);
        if (ObjectUtils.isNotEmpty(dispatchR.getData())) {
            TaskPickupDispatch dispatch = dispatchR.getData();
            TaskPickupDispatchDTO pickup = new TaskPickupDispatchDTO();
            BeanUtil.copyProperties(dispatch, pickup);
            /**
             * 如果快递员Id不存在,则代表没有快递员
             */
            if (StringUtils.isNotBlank(pickup.getCourierId())) {
                /**
                 * 查询快递员信息
                 */
                GlobalUser user = baseFeign.queryAccordingToTheCourierId(pickup.getCourierId());
                pickup.setCourierName(user.getName());
                pickup.setPhone(user.getMobile());

                /**
                 * 查询所在网点
                 */
                String name = baseFeign.findById(pickup.getAgencyId()).getData().getName();
                pickup.setAgencyName(name);
            }
            map.put("pickup", pickup);

        }

        /**
         * 获取派送信息
         */
        R<TaskPickupDispatch> r = workFeign.findByOrderId(orderId, 2);
        if (ObjectUtils.isNotEmpty(r.getData())) {
            TaskPickupDispatch dispatch = r.getData();
            TaskPickupDispatchDTO dto = new TaskPickupDispatchDTO();
            BeanUtil.copyProperties(dispatch, dto);
            /**
             * 如果快递员Id不存在,则代表没有快递员
             */
            if (StringUtils.isNotBlank(dto.getCourierId())) {
                /**
                 * 查询快递员信息
                 */
                GlobalUser user = baseFeign.queryAccordingToTheCourierId(dto.getCourierId());
                dto.setCourierName(user.getName());
                dto.setPhone(user.getMobile());
                /**
                 * 查询所在网点
                 */
                String name = baseFeign.findById(dto.getAgencyId()).getData().getName();
                dto.setAgencyName(name);
            }
            map.put("dispatch", dto);
        }
        map.put("goodsTypeName", data.getName());
        return new R<>(ResultCode.SUCCESS_CODE, map);
    }

    /**
     * 获取商品类型
     *
     * @param cargo
     * @return
     */
    private GoodsType getPdGoodsType(OrderCargo cargo) {
        R<GoodsType> r = this.baseFeign.getById(cargo.getGoodsTypeId());
        GoodsType data = r.getData();
        return data;
    }

    /**
     * 获取订单的运单信息
     *
     * @param orderId
     * @return
     */
    private TransportOrder getTransportOrder(String orderId) {
        R<TransportOrder> r = this.workFeign.getByOrderId(orderId);
        TransportOrder transportOrder = r.getData();
        return transportOrder;
    }

    /**
     * 根据订单id获取订单位置信息
     *
     * @param orderId
     * @return
     */
    private OrderLocation getOrderLocation(String orderId) {
        QueryWrapper<OrderLocation> orderLocationQueryWrapper = new QueryWrapper<>();
        orderLocationQueryWrapper.eq("order_id", orderId);
        OrderLocation location = this.orderLocationService.getOne(orderLocationQueryWrapper);
        return location;
    }

    /**
     * 根据订单id获取 订单重量信息
     *
     * @param orderId
     * @return
     */
    private OrderCargo getOrderCargo(String orderId) {
        QueryWrapper<OrderCargo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        OrderCargo cargo = this.orderCargoService.getOne(queryWrapper);
        return cargo;
    }

    /**
     * 订单修改(当订单只是处于下单状态时,才能修改)
     *
     * @param orderDTO
     * @return
     */
    @Override
    public R updateOrder(OrderDTO orderDTO) {
        notNull(orderDTO);
        if (orderDTO.getOrder().getStatus() != 1) {
            throw new TmsException(ResultCode.ERROR_CODE, "订单不可更改!");
        }
        this.baseMapper.updateById(orderDTO.getOrder());
        this.orderCargoService.updateById(orderDTO.getOrderCargo());
        this.orderLocationService.updateById(orderDTO.getOrderLocation());

        return new R(ResultCode.SUCCESS_CODE, "订单修改成功!");
    }

    /**
     * 订单删除 删除及它的一切数据
     * TODO
     *
     * @param orderId
     * @return
     */
    @Override
    public R deleteOrder(String orderId) {
        if (StringUtils.isBlank(orderId)) {
            throw new TmsException(ResultCode.SUCCESS_CODE, "订单Id为空!");
        }
        return null;
    }

    /***
     * 订单超时
     * @param orderId
     * @return
     */
    @Override
    public R changeOrderTimeout(String orderId) {
        Order order = new Order();
        order.setPaymentStatus(3);
        order.setId(orderId);
        int i = this.baseMapper.updateById(order);
        return i > 0 ? new R(ResultCode.SUCCESS_CODE, "修改成功!", true) : new R(ResultCode.ERROR_CODE, "修改失败", false);
    }

    /**
     * 支付成功,将订单设置为已支付
     *
     * @param orderId
     * @return
     */
    @Override
    public R paySuccess(String orderId) {
        Order order = new Order();
        order.setId(orderId);
        order.setPaymentStatus(2);
        int i = this.baseMapper.updateById(order);
        return i > 0 ? new R(ResultCode.SUCCESS_CODE, "订单支付成功!", true) : new R(ResultCode.ERROR_CODE, "订单支付失败!", false);
    }

    /**
     * 查询带取件并且是状态是已付的订单信息
     *
     * @param status        订单状态
     * @param paymentStatus 付款状态
     * @return
     */
    @Override
    public List<Order> getOrderList(Integer status, Integer paymentStatus) {
        if (null == status) {
            throw new TmsException(ResultCode.ERROR_CODE, "订单状态为空!");
        }
        if (null == paymentStatus) {
            throw new TmsException(ResultCode.ERROR_CODE, "付款状态为空!");
        }
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status);
        queryWrapper.eq("payment_status", paymentStatus);
        List<Order> orders = this.baseMapper.selectList(queryWrapper);
        return orders;
    }

    /**
     * 获取订单数据服务看板
     *
     * @param dateCount
     * @return
     */
    @Override
    public OrderDashboardVO getOrderDashboardData(Integer dateCount) {
        OrderDashboardVO orderDashboardVO = new OrderDashboardVO();
        /**
         * 获取进一周的订单数量折线图
         */
        List<String> dateTimes = new ArrayList<>();
        List<Long> list = getTheNumberOfDailyOrders(dateCount, dateTimes);
        if (CollUtil.isNotEmpty(list)) {
            orderDashboardVO.setDailyOrders(list);
            log.info("每日订单数设置成功!");
        }
        if (CollUtil.isNotEmpty(dateTimes)) {
            orderDashboardVO.setDateTimes(dateTimes);
            log.info("设置每日订单日期成功!");
        }

        /**
         * 订单总数
         */
        Integer orderTotal = baseMapper.selectCount(null);
        orderDashboardVO.setOrderTotal(Long.valueOf(orderTotal));

        /**
         * 下订单总数
         */
        QueryWrapper<Order> orderQuery = new QueryWrapper<>();
        orderQuery.eq("status", 1);
        orderDashboardVO.setPlaceAnOrderCount(Long.valueOf(this.count(orderQuery)));
        /**
         * 网点自寄
         */
        orderQuery = new QueryWrapper<>();
        orderQuery.eq("status", 2);
        orderDashboardVO.setSelfDeliveryFromOutletsCount(Long.valueOf(count(orderQuery)));
        /**
         * 已取件
         */
        orderQuery = new QueryWrapper<>();
        orderQuery.eq("status", 3);
        orderDashboardVO.setPickedUpCount(Long.valueOf(count(orderQuery)));
        /**
         * 网点入库
         */
        orderQuery = new QueryWrapper<>();
        orderQuery.eq("status", 4);
        orderDashboardVO.setOutletStorageCount(Long.valueOf(count(orderQuery)));
        /**
         * 运输中
         */
        orderQuery = new QueryWrapper<>();
        orderQuery.eq("status", 5);
        orderDashboardVO.setInTransitCount(Long.valueOf(count(orderQuery)));
        /**
         * 网点出库
         */
        orderQuery = new QueryWrapper<>();
        orderQuery.eq("status", 6);
        orderDashboardVO.setOutletCount(Long.valueOf(count(orderQuery)));
        /**
         * 派送中
         */
        orderQuery = new QueryWrapper<>();
        orderQuery.eq("status", 7);

        orderDashboardVO.setDeliveringCount(Long.valueOf(count(orderQuery)));
        /**
         * 签收
         */
        orderQuery = new QueryWrapper<>();
        orderQuery.eq("status", 8);
        orderDashboardVO.setSignForCount(Long.valueOf(count(orderQuery)));
        /**
         * 取件
         */

        orderQuery = new QueryWrapper<>();
        orderQuery.eq("status", 9);
        orderDashboardVO.setPickupCount(Long.valueOf(orderTotal));
        /**
         * 预结
         */
        orderQuery = new QueryWrapper<>();
        orderQuery.eq("payment_method", 1);
        orderDashboardVO.setPreClosingCount(Long.valueOf(count(orderQuery)));
        /**
         * 到付
         */
        orderQuery = new QueryWrapper<>();
        orderQuery.eq("payment_method", 2);
        orderDashboardVO.setToPayCount(Long.valueOf(count(orderQuery)));
        /**
         * 未付
         */
        orderQuery = new QueryWrapper<>();
        orderQuery.eq("payment_status", 1);
        orderDashboardVO.setUnpaidCount(Long.valueOf(count(orderQuery)));
        /**
         * 已付
         */
        orderQuery = new QueryWrapper<>();
        orderQuery.eq("payment_status", 2);
        orderDashboardVO.setPaidCount(Long.valueOf(count(orderQuery)));
        /**
         * 订单超时
         */
        orderQuery = new QueryWrapper<>();
        orderQuery.eq("payment_status", 3);
        orderDashboardVO.setPayTimeOutCount(Long.valueOf(count(orderQuery)));
        /**
         * 同城
         */
        orderQuery = new QueryWrapper<>();
        orderQuery.eq("order_type", 1);
        orderDashboardVO.setSameCityCount(Long.valueOf(count(orderQuery)));
        /**
         * 城际
         */
        orderQuery = new QueryWrapper<>();
        orderQuery.eq("order_type", 2);
        orderDashboardVO.setIntercityCount(Long.valueOf(count(orderQuery)));
        /**
         * 上门取件
         */
        orderQuery = new QueryWrapper<>();
        orderQuery.eq("pickup_type", 2);
        orderDashboardVO.setPickUpCount(Long.valueOf(count(orderQuery)));
        return orderDashboardVO;


    }

    /**
     * 获取每日订单数
     *
     * @param dateCount
     * @param dateTimes
     * @return
     */
    public List<Long> getTheNumberOfDailyOrders(Integer dateCount, List<String> dateTimes) {
        List<Long> list = new ArrayList<>();

        /**
         * 获取当前区域 默认为上海
         */
        ZoneId zoneId = ZoneId.systemDefault();
        /**
         * 获取当前时间
         */
        for (Integer i = dateCount; i > 0; i--) {
            LocalDate now = LocalDate.now().minusDays(i);
            /**
             * 添加日期
             */
            dateTimes.add(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

            ZonedDateTime zonedDateTime = now.atStartOfDay(zoneId);
            Date startDate = Date.from(zonedDateTime.toInstant());
            LocalDate endNow = LocalDate.now().minusDays(i - 1);
            ZonedDateTime endDateTime = endNow.atStartOfDay(zoneId);
            Date endDate = Date.from(endDateTime.toInstant());
            QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
            log.info("开始日期为:{}", now);
            log.info("结束日期为:{}", endNow);
            queryWrapper.between("create_time", startDate, endDate);
            Integer count = this.baseMapper.selectCount(queryWrapper);
            list.add(Long.valueOf(count));
        }
        return list;
    }


    /**
     * 订单重量信息添加
     *
     * @param orderDTO
     * @param order
     * @param transportOrderDTO
     */
    private void saveOrderCargo(OrderDTO orderDTO, Order order, TransportOrder transportOrderDTO) {
        /**
         * 4.添加订单重量表
         */
        OrderCargo orderCargo = new OrderCargo();
        BeanUtil.copyProperties(orderDTO.getOrderCargo(), orderCargo);
        orderCargo.setOrderId(order.getId());
        orderCargo.setTranOrderId(transportOrderDTO.getId());
        orderCargoService.save(orderCargo);
    }

    /**
     * 订单位置添加
     *
     * @param orderDTO
     * @param order
     */
    private void saveOrderLocation(OrderDTO orderDTO, Order order) {
        /**
         * 3.添加订单位置信息表
         */
        OrderLocation orderLocation = new OrderLocation();
        BeanUtil.copyProperties(orderDTO.getOrderLocation(), orderLocation);
        orderLocation.setOrderId(order.getId());
        orderLocation.setStatus("1");
        orderLocationService.save(orderLocation);
    }

    /**
     * 运单添加
     *
     * @param order
     * @return
     */
    @NotNull
    private TransportOrder saveTransportOrder(Order order) {
        /**
         * 2.添加运单表
         */
        TransportOrderDTO transportOrderDTO = new TransportOrderDTO();

        transportOrderDTO.setOrderId(order.getId());
        transportOrderDTO.setCreateTime(new Date());
        /**
         * 新建
         */
        transportOrderDTO.setStatus(1);
        /**
         * 带调度
         */
        transportOrderDTO.setSchedulingStatus(1);
        R<TransportOrder> r = workFeign.save(transportOrderDTO);
        return r.getData();
    }

    /**
     * 订单添加
     *
     * @param orderDTO
     * @return
     */
    @NotNull
    private Order saveOrder(OrderDTO orderDTO) {
        /**
         * 1.添加订单表
         */
        Order order = new Order();
        BeanUtil.copyProperties(orderDTO.getOrder(), order);
        order.setStatus(1);
        order.setCreateTime(new Date());
        /**
         * 设置订单为未支付状态,发送消息
         */
        order.setPaymentStatus(1);
        this.baseMapper.insert(order);
        return order;
    }

    /**
     * 获取金额
     *
     * @param orderDTO
     */
    private void getAmount(OrderDTO orderDTO) {
        List<LinkedHashMap<String, Object>> linkedHashMaps = getDistance(orderDTO);


        String distance = (String) linkedHashMaps.get(0).get("distance");
        log.info("距离为:" + distance);
        AddressRule rule = new AddressRule();

        rule.setTotalWeight(orderDTO.getOrderCargo().getTotalWeight().doubleValue());

        AddressResult addressResult = new AddressResult();
        rule.setDistance(new BigDecimal(distance).divide(new BigDecimal(1000)).doubleValue());
        /**
         * 获取距离之后,进行规则匹配
         */
        KieSession kieSession = kieContainer.newKieSession();
        /**
         * 插入工作内存中
         */
        kieSession.insert(rule);
        kieSession.insert(addressResult);
        int i = kieSession.fireAllRules();
        log.info("匹配了==={}===条规则", i);
        kieSession.dispose();
        if (addressResult.isPostCodeResult()) {
            log.info("执行了规则,价格为:{}", addressResult.getResult());
            orderDTO.getOrder().setAmount(new BigDecimal(addressResult.getResult()));
        }
    }


    /**
     * 获取距离
     *
     * @param orderDTO
     * @return
     */
    private List<LinkedHashMap<String, Object>> getDistance(OrderDTO orderDTO) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            StringBuilder strUrl = new StringBuilder("https://restapi.amap.com/v3/distance");
            strUrl.append("?origins=");
            strUrl.append(orderDTO.getBegin());
            strUrl.append("&destination=");
            strUrl.append(orderDTO.getEnd());
            strUrl.append("&output=json");
            strUrl.append("&key=");
            strUrl.append(secret);
            System.out.println("strUrl = " + strUrl.toString());
            Map<String, Object> objectMap = HttpUrlUtils.sendUrl(strUrl.toString());
            String message = (String) objectMap.get("message");
            Map map = objectMapper.readValue(message, Map.class);
            if ("0".equals(map.get("status"))) {
                throw new TmsException(ResultCode.ERROR_CODE, "获取距离失败!");
            }
            List<LinkedHashMap<String, Object>> linkedHashMaps = (List<LinkedHashMap<String, Object>>) map.get("results");
            return linkedHashMaps;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 非空判断
     *
     * @param orderDTO
     */
    private void notNull(OrderDTO orderDTO) {
        if (ObjectUtils.isEmpty(orderDTO)) {
            throw new TmsException(ResultCode.ERROR_CODE, "orderDTO为空!");
        }
        if (ObjectUtils.isEmpty(orderDTO.getOrder())) {
            throw new TmsException(ResultCode.ERROR_CODE, "order为空!");
        }
        if (ObjectUtils.isEmpty(orderDTO.getOrderLocation())) {
            throw new TmsException(ResultCode.ERROR_CODE, "orderLocation为null");
        }
        if (ObjectUtils.isEmpty(orderDTO.getOrderCargo())) {
            throw new TmsException(ResultCode.ERROR_CODE, "orderCargo为null");
        }
        if (StringUtils.isBlank(orderDTO.getBegin())) {
            throw new TmsException(ResultCode.ERROR_CODE, "begin为空!");
        }
        if (StringUtils.isBlank(orderDTO.getEnd())) {
            throw new TmsException(ResultCode.ERROR_CODE, "end为空!");
        }
        if (orderDTO.getOrder().getPickupType() == null) {
            throw new TmsException(ResultCode.ERROR_CODE, "取件类型不能为null");
        }
    }

    @Override
    public Map<String,Object>  getOrderListByMemberId(String memberId){
        if(null == memberId && "".equals(memberId)){
            throw new IllegalArgumentException("参数异常!");
        }
        Map<String,Object> map=new HashMap<>();
        List<Order> orderList = this.list(new QueryWrapper<Order>().eq("member_id", memberId));

        List<OrderCargo> cargos=new ArrayList<>();

        if(CollUtil.isNotEmpty(orderList)){
            for(Order o: orderList){
                OrderCargo orderCargo = this.getOrderCargo(o.getId());
                cargos.add(orderCargo);
            }
        }
        map.put("orderList",orderList);
        map.put("cargos",cargos);
        return map;
    }
}
